package bepviet.android.engine;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper {

	private static String DB_Name ;
	private static String DB_Path ;
	private final Context mContext;
	private SQLiteDatabase myData;

	// private SQLiteDatabase myDataBase;

	public DataBaseHelper(Context context,String DB_Path,String DB_Name) {
		super(context, DB_Name, null, 1);
		this.mContext = context;
		this.DB_Path = DB_Path;
		this.DB_Name = DB_Name;
		
	}
		
	
	public void Check_CopyDataBase() {
		boolean ExistDB = CheckDataBase();
		if (ExistDB) {
			Log.d("TAG", "DataBase already exist!");
		} else {
			this.getReadableDatabase();
			try {
				CopyDataBase();
			} catch (Exception ex) {
				Log.d("TAG", "xxxx");
			}
		}

	}

	// =============================================================================

	protected boolean CheckDataBase() {
		SQLiteDatabase CheckDB = null;
		try {
			String myPath = DB_Path + DB_Name;
	//		this.mContext.deleteDatabase(DB_Name);
			CheckDB = SQLiteDatabase.openDatabase(myPath, null,
					SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
		} catch (SQLException ex) {
			return false;
		}
		if (CheckDB != null)
			CheckDB.close();
		return CheckDB != null ? true : false;
	}

	private void CopyDataBase() throws IOException {
		File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+DB_Name);		
		InputStream myInput = new FileInputStream(file);
		String outFileName = DB_Path + DB_Name;
		OutputStream myOutput = new FileOutputStream(outFileName);
		byte[] buffer = new byte[1024];
		int lenght;
		while ((lenght = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, lenght);

		}
		myOutput.flush();
		myOutput.close();
		myInput.close();

	}

	// =============================================================================

	public void OpenDataBase() {
		String myPath = DB_Path + DB_Name;
		 myData = SQLiteDatabase.openDatabase(myPath, null,
		 SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
	}

	public synchronized void close() {
		if (myData != null)
			myData.close();
		super.close();
	}

	public Cursor QueryData(String query) throws SQLException {
		return this.myData.rawQuery(query, null);
	}


	@Override
	public void onCreate(SQLiteDatabase db) {
		this.Check_CopyDataBase();
		
	}


	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}

}
